/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Model.AirMarshall;
import Model.Nationaliteit;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import Util.StringToHtmlConverter;

/**
 *
 * @author ryanradder
 */
public class CreateChangeAirMarshallView extends javax.swing.JInternalFrame {

    /**
     * Creates new form CreateChangeAirMarshallView
     */
    ArrayList<String> errors = new ArrayList();
    boolean newMarshall;
    int currentid;

    public CreateChangeAirMarshallView() {
        initComponents();
        cboNationaliteit.setModel(new DefaultComboBoxModel(Nationaliteit.values()));
        newMarshall = true;
        this.setTitle("Create AirMarshall");
    }

    public CreateChangeAirMarshallView(AirMarshall selected_marshall) {
        initComponents();
        cboNationaliteit.setModel(new DefaultComboBoxModel(Nationaliteit.values()));
        txtNaam.setText(selected_marshall.getNaam());
        txtPersoneelsnummer.setText(String.valueOf(selected_marshall.getPersoneelsnummer()));
        txtWapennummer.setText(String.valueOf(selected_marshall.getWapennummer()));
        cboNationaliteit.setSelectedItem(selected_marshall.getNationaliteit());
        newMarshall = false;
        currentid = selected_marshall.getId();
        this.setTitle("Change AirMarshall");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblNaam = new javax.swing.JLabel();
        lblWapennummer = new javax.swing.JLabel();
        lblNationaliteit = new javax.swing.JLabel();
        lblPersoneelsnummer = new javax.swing.JLabel();
        txtNaam = new javax.swing.JTextField();
        txtWapennummer = new javax.swing.JTextField();
        txtPersoneelsnummer = new javax.swing.JTextField();
        cboNationaliteit = new javax.swing.JComboBox();
        ErrorLabel = new javax.swing.JLabel();
        btnSave = new javax.swing.JButton();
        errorLabel = new javax.swing.JLabel();
        btnClose = new javax.swing.JButton();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(flyaway.FlyAWayApp.class).getContext().getResourceMap(CreateChangeAirMarshallView.class);
        lblNaam.setText(resourceMap.getString("lblNaam.text")); // NOI18N
        lblNaam.setName("lblNaam"); // NOI18N

        lblWapennummer.setText(resourceMap.getString("lblWapennummer.text")); // NOI18N
        lblWapennummer.setName("lblWapennummer"); // NOI18N

        lblNationaliteit.setText(resourceMap.getString("lblNationaliteit.text")); // NOI18N
        lblNationaliteit.setName("lblNationaliteit"); // NOI18N

        lblPersoneelsnummer.setText(resourceMap.getString("lblPersoneelsnummer.text")); // NOI18N
        lblPersoneelsnummer.setName("lblPersoneelsnummer"); // NOI18N

        txtNaam.setText(resourceMap.getString("txtNaam.text")); // NOI18N
        txtNaam.setName("txtNaam"); // NOI18N

        txtWapennummer.setText(resourceMap.getString("txtWapennummer.text")); // NOI18N
        txtWapennummer.setName("txtWapennummer"); // NOI18N

        txtPersoneelsnummer.setText(resourceMap.getString("txtPersoneelsnummer.text")); // NOI18N
        txtPersoneelsnummer.setName("txtPersoneelsnummer"); // NOI18N

        cboNationaliteit.setName("cboNationaliteit"); // NOI18N

        ErrorLabel.setText(resourceMap.getString("ErrorLabel.text")); // NOI18N
        ErrorLabel.setName("ErrorLabel"); // NOI18N

        btnSave.setText(resourceMap.getString("btnSave.text")); // NOI18N
        btnSave.setName("btnSave"); // NOI18N
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        errorLabel.setText(resourceMap.getString("errorLabel.text")); // NOI18N
        errorLabel.setName("errorLabel"); // NOI18N

        btnClose.setText(resourceMap.getString("btnClose.text")); // NOI18N
        btnClose.setName("btnClose"); // NOI18N
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .addContainerGap()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                    .add(lblPersoneelsnummer, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 97, Short.MAX_VALUE)
                                    .add(lblNaam, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .add(lblWapennummer, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .add(lblNationaliteit, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .add(layout.createSequentialGroup()
                                .add(11, 11, 11)
                                .add(ErrorLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(txtWapennummer, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)
                            .add(txtNaam, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)
                            .add(txtPersoneelsnummer, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE)
                            .add(cboNationaliteit, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(errorLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                .add(btnSave)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(btnClose)
                                .add(0, 0, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblNaam)
                    .add(txtNaam, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblWapennummer)
                    .add(txtWapennummer, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblPersoneelsnummer)
                    .add(txtPersoneelsnummer, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(8, 8, 8)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lblNationaliteit)
                    .add(cboNationaliteit, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btnSave)
                    .add(btnClose))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(errorLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 18, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(ErrorLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 0, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    
    private boolean save(){
             
             AirMarshall toAdd = new AirMarshall();
             errors.clear();
             
            String naam = txtNaam.getText();
          int persnmr  =-1;
          int wapennmr =-1;
          
          try{
           
            wapennmr = Integer.parseInt(txtWapennummer.getText());
            persnmr = Integer.parseInt(txtPersoneelsnummer.getText());
           
            }catch(Exception ex){       
            }
            if(naam.isEmpty()){
                errors.add("Please enter a correct name");
            }
            if(persnmr == -1 ){
                errors.add("Enter a correct staffnumber");
            }
            if(wapennmr == -1 ){
                errors.add("enter a valid weapon id");
            }
            if(persnmr <-1){
                errors.add("the personeelsnummer cant be a negative number");
            }if(wapennmr < -1){
                errors.add("the wapennummer cant be a negative number");
            }
                
         
             if(errors.isEmpty()){
       toAdd.setNaam(naam);
       toAdd.setWapennummer(wapennmr);
       toAdd.setPersoneelsnummer(persnmr);
       toAdd.setNationaliteit((Nationaliteit) cboNationaliteit.getSelectedItem());
       toAdd.setId(currentid);
       
       if(newMarshall){
               try{
                    Controller.Controller.Instance().save(toAdd);
                    this.dispose();
                } catch (Exception ex) {
                }
            } else {
                try {
                    Controller.Controller.Instance().update(toAdd);
                    this.dispose();
                } catch (Exception ex) {
                }
            }
        } else {

            return false;
        }


        return true;
    }

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        if (this.save()) {
        } else {
            errorLabel.setText(StringToHtmlConverter.format(errors));
        }
    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
     this.dispose();
    }//GEN-LAST:event_btnCloseActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CreateChangeAirMarshallView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CreateChangeAirMarshallView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CreateChangeAirMarshallView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CreateChangeAirMarshallView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CreateChangeAirMarshallView().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel ErrorLabel;
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnSave;
    private javax.swing.JComboBox cboNationaliteit;
    private javax.swing.JLabel errorLabel;
    private javax.swing.JLabel lblNaam;
    private javax.swing.JLabel lblNationaliteit;
    private javax.swing.JLabel lblPersoneelsnummer;
    private javax.swing.JLabel lblWapennummer;
    private javax.swing.JTextField txtNaam;
    private javax.swing.JTextField txtPersoneelsnummer;
    private javax.swing.JTextField txtWapennummer;
    // End of variables declaration//GEN-END:variables
}
